﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public partial class FormLayPointSprinkler : System.Windows.Forms.Form
	{
		public int CurrentBtn
		{
			get
			{
				return this.currentBtn;
			}
		}

		public FamilySymbol Fsymbol { get; set; }

		public FormLayPointSprinkler(Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = doc;
			base.Location = new System.Drawing.Point(100, 100);
			base.Icon = Resource.YArchitechIcon;
		}

		private void FormLaySprinklerHead_Load(object sender, EventArgs e)
		{
			this.InitCurrentProject();
			this.btnHy.BackColor = System.Drawing.Color.White;
			this.currentBtn = 0;
			this.btnproj.BackColor = System.Drawing.Color.Transparent;
			this.panelHy.Visible = true;
			this.panelProj.Visible = false;
			this.initParameterSetting();
			switch (this.familyCategory)
			{
			case 1:
				this.rbtUp.Checked = true;
				break;
			case 2:
				this.rbtDown.Checked = true;
				break;
			case 3:
				this.rbtCe.Checked = true;
				break;
			case 4:
				this.rbtShuimu.Checked = true;
				break;
			case 5:
				this.rbtUpDown.Checked = true;
				break;
			}
			int num = this.sprinlkerLocationType;
			if (num != 1)
			{
				if (num == 2)
				{
					this.rdoOffset.Checked = true;
				}
			}
			else
			{
				this.rdoClick.Checked = true;
			}
			this.GetfamilyNameFromData();
			if (this.lbFamily.Items.Count > 0)
			{
				this.lbFamily.SelectedItem = this.familyName;
			}
			if (this.lbSymbol.Items.Count > 0)
			{
				this.lbSymbol.SelectedItem = this.familySymbol;
			}
		}

		private void InitCurrentProject()
		{
			this.tvwProject.Nodes.Add("喷头");
			HyProjectFamily.BingTvwFamilysTwoLevel(HyProjectFamily.GetFamilys(BuiltInCategory.OST_Sprinklers, this.doc), this.tvwProject);
		}

		private void GetSprinklerPara()
		{
			if (this.currentBtn == 1 && this.Fsymbol == null)
			{
				YJKMessageBox.Show("选择的族类型不能为空");
				return;
			}
			if (!(this.tbLevel.Text.Trim() != "") || !(this.tbRadius.Text.Trim() != "") || !(this.tbK.Text.Trim() != "") || !(this.lbSymbol.SelectedItem.ToString() != "") || this.id <= 0 || (!(this.tbDistance.Text.Trim() != "") && !this.rdoClick.Checked))
			{
				YJKMessageBox.Show("参数都不能为空");
				return;
			}
			if (double.Parse(this.tbRadius.Text.Trim()) == 0.0)
			{
				YJKMessageBox.Show("喷淋半径不能为0");
				return;
			}
			if (this.rdoOffset.Checked && double.Parse(this.tbDistance.Text.Trim()) < 20.0)
			{
				YJKMessageBox.Show("偏移距离过小，无法布置");
				return;
			}
			this.sprinklersPara = new SprinklersParam();
			this.sprinklersPara.OffSet = double.Parse(this.tbLevel.Text.Trim());
			this.sprinklersPara.Radius = double.Parse(this.tbRadius.Text.Trim());
			this.sprinklersPara.Range = this.checkBox.Checked;
			this.sprinklersPara.Kratio = double.Parse(this.tbK.Text.Trim());
			this.sprinklersPara.SymbolName = this.lbSymbol.SelectedItem.ToString();
			if (this.tbDistance.Text.Trim() != "")
			{
				this.sprinklersPara.SprinlkerDistance = double.Parse(this.tbDistance.Text.Trim());
			}
			this.sprinklersPara.Id = this.id;
		}

		private void btnLayOne_Click(object sender, EventArgs e)
		{
			this.GetSprinklerPara();
			if (this.sprinklersPara != null)
			{
				base.DialogResult = DialogResult.OK;
			}
		}

		private void initParameterSetting()
		{
			UserSetting userSetting = new UserSetting();
			if (userSetting.GetValue("sprinklers", "familyCategory") == "")
			{
				this.familyCategory = 1;
			}
			else
			{
				this.familyCategory = int.Parse(userSetting.GetValue("sprinklers", "familyCategory"));
			}
			this.familyName = userSetting.GetValue("sprinklers", "familyName");
			this.familySymbol = userSetting.GetValue("sprinklers", "familySymbol");
			this.tbLevel.Text = userSetting.GetValue("sprinklers", "offset");
			if (userSetting.GetValue("sprinklers", "sprinlkerLocationType") == "")
			{
				this.sprinlkerLocationType = 1;
			}
			else
			{
				this.sprinlkerLocationType = int.Parse(userSetting.GetValue("sprinklers", "sprinlkerLocationType"));
			}
			this.tbDistance.Text = userSetting.GetValue("sprinklers", "sprinlkerDistance");
			try
			{
				this.checkBox.Checked = Convert.ToBoolean(userSetting.GetValue("sprinklers", "range"));
			}
			catch (Exception)
			{
			}
		}

		private void UserSettingWrite()
		{
			this.tbLevel.Text = StringTools.RemoveZero(this.tbLevel.Text.Trim());
			try
			{
				UserSetting userSetting = new UserSetting();
				userSetting.WriteValue("sprinklers", "familyCategory", this.familyCategory.ToString());
				userSetting.WriteValue("sprinklers", "familyName", this.lbFamily.SelectedItem.ToString());
				userSetting.WriteValue("sprinklers", "familySymbol", this.lbSymbol.SelectedItem.ToString());
				userSetting.WriteValue("sprinklers", "offset", this.tbLevel.Text);
				userSetting.WriteValue("sprinklers", "range", this.checkBox.Checked.ToString());
				userSetting.WriteValue("sprinklers", "sprinlkerLocationType", this.sprinlkerLocationType.ToString());
				userSetting.WriteValue("sprinklers", "sprinlkerDistance", this.tbDistance.Text);
				this.SaveParasFofDB();
			}
			catch (Exception)
			{
			}
		}

		private void SaveParasFofDB()
		{
			List<ListItem> list = new List<ListItem>();
			list.Add(new ListItem("RadiuDiameter", this.sprinklersPara.Radius.ToString()));
			list.Add(new ListItem("KCoefficient", this.sprinklersPara.Kratio.ToString()));
			if (!this.sfdr.SetInfoById("Sprinkler", this.sprinklersPara.Id, list))
			{
				YJKMessageBox.Show("保存数据失败");
			}
		}

		private void rbtUp_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbtUp.Checked)
			{
				this.familyCategory = 1;
				this.GetfamilyNameFromData();
				this.tbLevel.Enabled = true;
			}
		}

		private void rbtDown_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbtDown.Checked)
			{
				this.familyCategory = 2;
				this.GetfamilyNameFromData();
				this.tbLevel.Enabled = true;
			}
		}

		private void rbtCe_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbtCe.Checked)
			{
				this.familyCategory = 3;
				this.GetfamilyNameFromData();
				this.tbLevel.Enabled = true;
			}
		}

		private void rbtShuimu_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbtShuimu.Checked)
			{
				this.familyCategory = 4;
				this.GetfamilyNameFromData();
				this.tbLevel.Enabled = true;
			}
		}

		private void rbtUpDown_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbtUpDown.Checked)
			{
				this.familyCategory = 5;
				this.GetfamilyNameFromData();
			}
			if (this.rbtUpDown.Checked && this.rdoClick.Checked)
			{
				this.tbLevel.Enabled = false;
				return;
			}
			this.tbLevel.Enabled = true;
		}

		private void GetfamilyNameFromData()
		{
			this.lbFamily.Items.Clear();
			this.listname = this.sfdr.GetAllName("Sprinkler", this.familyCategory);
			bool flag = false;
			if (this.listname.Count > 0)
			{
				int num = 0;
				int selectedIndex = 0;
				foreach (ListItem listItem in this.listname)
				{
					this.lbFamily.Items.Add(listItem);
					if (listItem.Name == this.familyName)
					{
						selectedIndex = num;
						flag = true;
					}
					num++;
				}
				if (!flag)
				{
					this.lbFamily.SelectedIndex = 0;
					return;
				}
				this.lbFamily.SelectedIndex = selectedIndex;
			}
		}

		private void lbFamily_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.lbFamily.SelectedItem == null)
			{
				return;
			}
			SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
			this.id = 0;
			if (this.listname.Count > 0)
			{
				foreach (ListItem listItem in this.listname)
				{
					if (this.lbFamily.SelectedItem.ToString() == listItem.Name)
					{
						this.id = int.Parse(listItem.Tag.ToString());
					}
				}
			}
			foreach (ListItem listItem2 in spaceFamilyDataReader.GetInfoById("Sprinkler", this.id))
			{
				if (listItem2.Name == "Image")
				{
					if (File.Exists(listItem2.Value))
					{
						this.picbox.Image = new Bitmap(listItem2.Value);
					}
					else
					{
						this.picbox.Image = null;
					}
				}
				else if (listItem2.Name == "FamilySymbol")
				{
					this.UptadaFamilySybol(listItem2.Value);
				}
				else if (listItem2.Name == "RadiuDiameter")
				{
					this.tbRadius.Text = listItem2.Value;
				}
				else if (listItem2.Name == "KCoefficient")
				{
					this.tbK.Text = listItem2.Value;
				}
			}
		}

		private void UptadaFamilySybol(string szData)
		{
			this.lbSymbol.Items.Clear();
			foreach (string text in szData.Split(new char[]
			{
				';',
				',',
				'，'
			}))
			{
				if (text.Trim() != "")
				{
					this.lbSymbol.Items.Add(text);
				}
			}
			if (this.lbSymbol.Items != null && this.lbSymbol.Items.Count > 0)
			{
				this.lbSymbol.SelectedIndex = 0;
			}
		}

		private void btnCancle_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void FormLaySprinklerHead_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.currentBtn == 1)
			{
				return;
			}
			this.UserSettingWrite();
		}

		private void rdoClick_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoClick.Checked)
			{
				this.sprinlkerLocationType = 1;
				this.tbDistance.Enabled = false;
			}
			if (this.rbtUpDown.Checked && this.rdoClick.Checked)
			{
				this.tbLevel.Enabled = false;
				return;
			}
			this.tbLevel.Enabled = true;
		}

		private void rdoOffset_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoOffset.Checked)
			{
				this.sprinlkerLocationType = 2;
				this.tbDistance.Enabled = true;
				this.tbLevel.Enabled = true;
			}
		}

		private void tvwProject_AfterSelect(object sender, TreeViewEventArgs e)
		{
			if (this.tvwProject.SelectedNode == null)
			{
				return;
			}
			this.tvwProject.SelectedNode.BackColor = System.Drawing.Color.DodgerBlue;
			this.tvwProject.SelectedNode.ForeColor = System.Drawing.Color.White;
			if (this.tvwProject.SelectedNode.Level == 0)
			{
				this.lbSymbol.Items.Clear();
				return;
			}
			Family family = this.tvwProject.SelectedNode.Tag as Family;
			string imageByFamily = HyProjectFamily.GetImageByFamily(family, "Sprinkler", this.doc);
			if (File.Exists(imageByFamily))
			{
				this.picbox.Image = Image.FromFile(imageByFamily);
			}
			else
			{
				this.picbox.Image = null;
			}
			this.lbSymbol.Items.Clear();
			this.fsList.Clear();
			this.fsList = family.GetSymbols();
			if (this.fsList.Count < 1)
			{
				return;
			}
			foreach (FamilySymbol familySymbol in this.fsList)
			{
				this.lbSymbol.Items.Add(familySymbol.Name);
			}
			this.lbSymbol.SelectedIndex = 0;
		}

		private void tvwProject_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			if (this.tvwProject.SelectedNode != null)
			{
				this.tvwProject.SelectedNode.BackColor = System.Drawing.Color.White;
				this.tvwProject.SelectedNode.ForeColor = System.Drawing.Color.Black;
			}
		}

		private void btnHy_Click(object sender, EventArgs e)
		{
			int selectedIndex = this.lbFamily.SelectedIndex;
			this.lbFamily.SelectedItem = null;
			this.btnHy.BackColor = System.Drawing.Color.White;
			this.currentBtn = 0;
			this.btnproj.BackColor = System.Drawing.Color.Transparent;
			this.panelHy.Visible = true;
			this.panelProj.Visible = false;
			if (this.lbFamily.Items.Count > 0)
			{
				this.lbFamily.SelectedIndex = selectedIndex;
			}
			if (this.lbSymbol.Items.Count > 0)
			{
				this.lbSymbol.SelectedItem = this.familySymbol;
			}
		}

		private void btnproj_Click(object sender, EventArgs e)
		{
			this.btnHy.BackColor = System.Drawing.Color.Transparent;
			this.currentBtn = 1;
			this.btnproj.BackColor = System.Drawing.Color.White;
			this.panelHy.Visible = false;
			this.panelProj.Visible = true;
			if (this.tvwProject.SelectedNode != null)
			{
				if (this.tvwProject.SelectedNode == this.tvwProject.TopNode)
				{
					this.lbSymbol.Items.Clear();
					return;
				}
				TreeNode selectedNode = this.tvwProject.SelectedNode;
				this.tvwProject.SelectedNode = this.tvwProject.Nodes[0];
				if (this.tvwProject.Nodes[0].Nodes.Count < 1)
				{
					return;
				}
				this.tvwProject.SelectedNode = selectedNode;
				return;
			}
			else
			{
				this.tvwProject.SelectedNode = this.tvwProject.Nodes[0];
				if (this.tvwProject.Nodes[0].Nodes.Count < 1)
				{
					return;
				}
				this.tvwProject.SelectedNode = this.tvwProject.Nodes[0].Nodes[0];
				return;
			}
		}

		private void lbSymbol_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.currentBtn == 1)
			{
				this.Fsymbol = this.fsList.FirstOrDefault((FamilySymbol q) => q.Name == this.lbSymbol.SelectedItem.ToString());
			}
		}

		public SprinklersParam sprinklersPara;

		public int familyCategory = 1;

		public int sprinlkerLocationType = 1;

		private string familyName = "";

		private string familySymbol = "";

		private int id;

		private List<ListItem> listname = new List<ListItem>();

		private SpaceFamilyDataReader sfdr = new SpaceFamilyDataReader();

		private Document doc;

		private int currentBtn;

		private List<FamilySymbol> fsList = new List<FamilySymbol>();
	}
}
